<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="isLoading">
        <!-- 用了一个行列布局 -->
        <tree-tools :node-data="company" :is-root="true" @add-depts="showAddDeptsDialog" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- v-on=>@ v-bind=> v-slot=>#  -->
          <template #default="{data}">
            <!-- 当el-row 放到其他标签中，宽度是通过内容来撑开的 -->
            <tree-tools
              :node-data="data"
              @del-depts="getDepartmentList"
              @add-depts="showAddDeptsDialog"
              @edit-depts="showEditDeptsDialog"
            />
          </template>
        </el-tree>
      </el-card>
      <add-depts ref="addDepts" :departs-list="departsList" :show-dialog.sync="showDialog" :node-data="nodeData" @add-depts-success="getDepartmentList" />
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import addDepts from './components/add-depts.vue'
import { getDepartmentListApi } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
export default {
  name: 'Departments',
  components: {
    treeTools,
    addDepts
  },
  data() {
    return {
      departs: [],
      departsList: [],
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      defaultProps: {
        label: 'name'
      },
      showDialog: false,
      nodeData: {},
      isLoading: false
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      this.isLoading = true
      const { data } = await getDepartmentListApi()
      this.company.name = data.companyName
      this.departsList = data.depts
      this.departs = transListToTreeData(data.depts, '')
      this.isLoading = false
    },
    closeDialog(val) {
      this.showDialog = val
    },
    showAddDeptsDialog(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      // this.$refs.addDepts.showTitle = '添加子部门'
      // this.$refs.addDepts.getSimpleUserList()
    },
    showEditDeptsDialog(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      // this.$refs.addDepts.showTitle = '修改部门'
      // this.$refs.addDepts.getSimpleUserList()
      this.$nextTick(() => {
        this.$refs.addDepts.getDepartmentDetail()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
